<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>机构详情</title>
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <!-- 可选的Bootstrap主题文件（一般不用引入） -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <link rel="stylesheet" href="/css/font-awesome.css">
    <link rel="stylesheet" href="/css/base.css">
    <link rel="stylesheet" href="/css/djs.css">

    <script src="/json/CityJson.js" type="text/javascript" charset="utf-8"></script>
    <script src="/json/ProJson.js" type="text/javascript" charset="utf-8"></script>
    <script src="/json/DistrictJson.js" type="text/javascript" charset="utf-8"></script>
    <script src="/js/user.js"></script>
    <script type="text/javascript" src="/js/base.js"></script>
</head>
<body>
<div id="top">

</div>
<div class="container">
    <div id="asideAffix" class="aside" data-spy="affix">
    </div>
    <div class="content centers editcontent" style="margin-top: 0;">
        <form role="form" id="editForm" class="form-horizontal">
            <div class="form-group">
                <div class="col-sm-12">
                    <label for="userName1" class="col-sm-2">用户姓名：</label>
                    <input type="text" id="userName1" required class="col-sm-4">
                </div>

                <div class="col-sm-12">
                    <label for="phone" class="col-sm-2">手机号码：</label>
                    <input type="text" id="phone" required class="col-sm-4">
                    <input type="hidden" id="userId">
                </div>

                <div class="col-sm-12" id="passwordDiv">
                    <label for="password" class="col-sm-2">密码：</label>
                    <input type="text" id="password" required class="col-sm-4">
                </div>


                <div class="col-sm-12">
                    <label for="UserRole" class="col-sm-2">用户权限：</label>
                    <select id="UserRole" class="col-sm-2" required>
                        <option value="1">仅供查看</option>
                        <option value="2">可以修改</option>
                    </select>
                </div>

                <div class="col-sm-12">
                    <label for="certificateType" class="col-sm-2">证件类型：</label>
                    <select id="certificateType" class="col-sm-2" required>
                        <option value="1">身份证</option>
                        <option value="2">驾驶证</option>
                    </select>
                </div>

                <div class="col-sm-12">
                    <label for="certificateCode" class="col-sm-2">证件号码：</label>
                    <input type="text" id="certificateCode" required class="col-sm-4">
                </div>

                <div class="col-sm-12" style="display: none;">
                    <!--<label for="orgSelect" class="col-sm-2">关联组织：</label>-->
                    <!--<select id="orgSelect" class="col-sm-2" required>-->
                        <!--<option value="">&#45;&#45;请选择组织&#45;&#45;</option>-->
                    <!--</select>-->
                    <input id="orgSelect"/>
                </div>
            </div>
            <div>
                <button type="button" class="col-sm-2 btn btn-primary" id="rollback"
                        style="border-color: #ffd632;background: #ffd632 linear-gradient(to bottom,#ffd632 0,#ffd632 100%);">
                    返回
                </button>
                <button class="col-sm-2 btn btn-primary" id="create"
                        style="border-color: #ffd632;background: #ffd632 linear-gradient(to bottom,#ffd632 0,#ffd632 100%);">
                    保存
                </button>
            </div>
        </form>

    </div>
</div>
</body>

<script type="text/javascript">
    $(function () {

        for(var i = 0;i<location.search.substr(1).split("&").length;i++){
            if("userId" == location.search.substr(1).split("&")[i].split("=")[0]){
                if (location.search.substr(1).split("=")[i+1] != undefined && location.search.substr(1).split("=")[1] != 0 ) {
                    getData(location.search.substr(1).split("=")[i+1], getOrgData);
                    $("#passwordDiv").hide();
                } else {
                    $("#passwordDiv").show();
                }
            }else if("orgId" == location.search.substr(1).split("&")[i].split("=")[0]){
                if (location.search.substr(1).split("&")[i].split("=")[1] != undefined && location.search.substr(1).split("&")[i].split("=")[1] != 0 ) {
                    $("#orgSelect").val(location.search.substr(1).split("&")[i].split("=")[1]);
                }
            }
        }

    });

    var orgId = 0;
    function getData(userId, orgDataPut) {
        $.ajax({
            url: '/user/info',// 跳转到 action
            headers: {'Content-type': 'application/json;charset=UTF-8'},//传入数据数据格式 json
            data: JSON.stringify({
                userId: userId
            }),
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.statusCode == 0) {
                    if (undefined != data.data) {
                        $("#userName1").val(data.data.userName);
                        $("#UserRole").val(data.data.role);
                        $("#password").val(data.data.password);
                        $("#userId").val(data.data.userId);
                        $("#phone").val(data.data.phone);
                        $("#certificateType").val(data.data.certificateType);
                        $("#certificateCode").val(data.data.certificateCode);
                        orgId = data.data.orgId;
                    }

                    orgDataPut(JSON.parse(localStorage.getItem("user")).orgId);
                } else {
                    alert(data.statusMessage);
                }
            },
            error: function () {
                // view("异常！");
                alert("用户未登录或者服务器连接异常！");
            }
        });
    }

    function getOrgData(userId) {
        $.ajax({
            url: '/org/list',// 跳转到 action
            headers: {'Content-type': 'application/json;charset=UTF-8'},//传入数据数据格式 json
            data: JSON.stringify({
                orgId: userId
            }),
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.statusCode == 0) {
                    for (var i = 0; i < data.data.length; i++) {
                        var option = "<option value='" + data.data[i].orgId + "'>" + data.data[i].orgName + "</option>";

                        $("#orgSelect").append(option);
                    }
                    $("#orgSelect").val(orgId);
                } else {
                    alert(data.statusMessage);
                }
            },
            error: function () {
                // view("异常！");
                alert("用户未登录或者服务器连接异常！");
                location.href = "/html/login/login.html";
            }
        });
    }

    $("#editForm").submit(
            function () {
                if(!areYouSure("")){
                    return false;
                }
                save();
                return false;
            }
    );

    function save() {
        $.ajax({
            url: '/user/save',// 跳转到 action
            headers: {'Content-type': 'application/json;charset=UTF-8'},//传入数据数据格式 json
            data: JSON.stringify({
                role:$("#UserRole").val(),
                userId: $("#userId").val(),
                userName: $("#userName1").val(),
                phone: $("#phone").val(),
                orgId: $("#orgSelect").val(),
                password: $("#password").val(),
                certificateType: $("#certificateType").val(),
                certificateCode: $("#certificateCode").val()
            }),
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.statusCode == 0) {
                    back();
                } else {
                    alert(data.statusMessage);
                }
            },
            error: function () {
                // view("异常！");
                alert("服务器感冒了1！");
            }
        });
    }

    $("#rollback").click(function () {
        back()
    });

    function back() {
        location.href = "/html/user/userList.html";
    }
</script>
</html>